package de.gwtprime.client.widgets;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.PrimeRequestFactory;
import de.gwtprime.client.event.ApplyTabHeightEvent;
import de.gwtprime.client.event.ArticleSectionOnTypeEvent;
import de.gwtprime.client.event.handler.ArticleSectionOnTypeEventHandler;
import de.gwtprime.client.presenter.VoteControlPresenterArticleSectionImpl;
import de.gwtprime.client.user.ACLManager;
import de.gwtprime.client.user.ACLManager.PrimeUIElement;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.util.FacebookWidget;
import de.gwtprime.client.util.GooglePlusWidget;
import de.gwtprime.shared.requestFactory.ArticleProxy;
import de.gwtprime.shared.requestFactory.ArticleSectionProxy;
import de.gwtprime.shared.requestFactory.request.ArticleSectionRequest;

public class ArticleSectionContentWidget extends Composite{

	ArticleSectionContentUiBinder uiBinder = GWT.create(ArticleSectionContentUiBinder.class);

	interface ArticleSectionContentUiBinder extends UiBinder<Widget, ArticleSectionContentWidget>{}

	@UiField HTML textLabel = new HTML();
	@UiField SimplePanel panel = new SimplePanel();
	@UiField Button editButton;
	@UiField Button deleteButton;
	@UiField SimplePanel editPanel;
	@UiField SimplePanel facebookPanel;
	@UiField SimplePanel googlePlusPanel;
	private ArticleSectionEditWidget editWidget;

	private ClientFactory clientFactory;

	private ArticleSectionProxy articleSection;

	public ArticleSectionContentWidget(final ArticleProxy article, final ArticleSectionProxy articleSection,
			final ClientFactory clientFactory){
		initWidget(uiBinder.createAndBindUi(this));

		this.clientFactory = clientFactory;
		this.articleSection = articleSection;
		facebookPanel.add(new FacebookWidget(articleSection));
		googlePlusPanel.add(new GooglePlusWidget(articleSection));
		textLabel.setHTML(AppUtils.editForClient(articleSection.getText(), articleSection.getImages()));
		//panel.add(new ArticleSectionControlWidget(articleSection, article, clientFactory));
		ArticleSectionControlWidget controlWidget = new ArticleSectionControlWidget(clientFactory);
		VoteControlPresenterArticleSectionImpl controlPresenter = new VoteControlPresenterArticleSectionImpl(articleSection, clientFactory, controlWidget);

		controlWidget.initWidget(controlPresenter);

		panel.add(controlWidget);
		onEditHandler(articleSection, clientFactory);
		deleteButton.setVisible(ACLManager.hasUserAccess(clientFactory.getUser(), PrimeUIElement.ArticleSectionDelete));
		deleteButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				deleteArticleSection(articleSection, clientFactory);
			}
		});



		
		editButton.setEnabled(ACLManager.hasUserAccess(clientFactory.getUser(), PrimeUIElement.ArticleSectionEdit));
		editButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				editPanel.clear();
				editWidget = new ArticleSectionEditWidget(ArticleSectionContentWidget.this.articleSection, article, clientFactory, ArticleSectionContentWidget.this);
				editPanel.add(editWidget);

				// TODO Currently all sections get the event. More efficient to only address correct parent?
				clientFactory.getEventBus().fireEvent(new ApplyTabHeightEvent()); 
			}
		});
		
	}


	/**
	 * Handles keystrokes (updates the content on the UI) if the article section is 
	 * edited in the article section edit widget.
	 * @param articleSection
	 * @param clientFactory
	 */
	private void onEditHandler(final ArticleSectionProxy articleSection,
			final ClientFactory clientFactory) {
		clientFactory.getEventBus().addHandler(ArticleSectionOnTypeEvent.TYPE, new ArticleSectionOnTypeEventHandler() {

			@Override
			public void onType(ArticleSectionOnTypeEvent event) {
				if(articleSection != null && articleSection.getId() == event.getId()){
					textLabel.setHTML(AppUtils.editForClient(event.getText(), event.getImages()));
					clientFactory.getEventBus().fireEvent(new ApplyTabHeightEvent());
				}
			}
		});
	}

	/**
	 * Delete an article section
	 * @param articleSection
	 * @param clientFactory
	 */
	private void deleteArticleSection(
			final ArticleSectionProxy articleSection,
			final ClientFactory clientFactory) {

		ArticleSectionContentWidget.this.setVisible(false);
		final PrimeRequestFactory requestFactory = clientFactory
				.getRequestFactory();
		final ArticleSectionRequest request = requestFactory
				.articleSectionRequest();
		Request<Void> createReq2 = request.remove().using(articleSection);

		createReq2.fire(new Receiver<Void>() {
			@Override
			public void onSuccess(Void list) {
				AppUtils.log("Removing article section was successful");
			}

			@Override
			public void onFailure(ServerFailure error) {
				AppUtils.log("Removing article section was failed");
			}
		});
	}
	
	public void setArticleSection(ArticleSectionProxy articleSection){
		this.articleSection = articleSection;
		
	}

}
